// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ai.states;

import java.util.HashMap;
import zombie.ai.State;
import zombie.audio.parameters.ParameterZombieState;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoZombie;
import zombie.gameStates.IngameState;
import zombie.iso.IsoChunk;
import zombie.iso.IsoUtils;
import zombie.iso.IsoWorld;
import zombie.network.GameServer;
import zombie.network.NetworkVariables;
import zombie.network.ServerMap;
import zombie.vehicles.PathFindBehavior2;
import zombie.vehicles.PolygonalMap2;

public class WalkTowardNetworkState extends State {
    static WalkTowardNetworkState _instance = new WalkTowardNetworkState();
    private static final Integer PARAM_TICK_COUNT = 2;

    public static WalkTowardNetworkState instance() {
        return _instance;
    }

    @Override
    public void enter(IsoGameCharacter owner) {
        HashMap hashMap = owner.getStateMachineParams(this);
        hashMap.put(PARAM_TICK_COUNT, IngameState.instance.numberTicks);
        owner.setVariable("bMoving", true);
        owner.setVariable("bPathfind", false);
    }

    @Override
    public void execute(IsoGameCharacter owner) {
        IsoZombie isoZombie = (IsoZombie)owner;
        PathFindBehavior2 pathFindBehavior2 = isoZombie.getPathFindBehavior2();
        isoZombie.vectorToTarget.x = isoZombie.networkAI.targetX - isoZombie.x;
        isoZombie.vectorToTarget.y = isoZombie.networkAI.targetY - isoZombie.y;
        pathFindBehavior2.walkingOnTheSpot.reset(isoZombie.x, isoZombie.y);
        if (isoZombie.z != (float)isoZombie.networkAI.targetZ
            || isoZombie.networkAI.predictionType != NetworkVariables.PredictionTypes.Thump
                && isoZombie.networkAI.predictionType != NetworkVariables.PredictionTypes.Climb) {
            if (isoZombie.z == (float)isoZombie.networkAI.targetZ
                && !PolygonalMap2.instance
                    .lineClearCollide(isoZombie.x, isoZombie.y, isoZombie.networkAI.targetX, isoZombie.networkAI.targetY, isoZombie.networkAI.targetZ, null)) {
                if (isoZombie.networkAI.usePathFind) {
                    pathFindBehavior2.reset();
                    isoZombie.setPath2(null);
                    isoZombie.networkAI.usePathFind = false;
                }

                pathFindBehavior2.moveToPoint(isoZombie.networkAI.targetX, isoZombie.networkAI.targetY, 1.0F);
                isoZombie.setVariable(
                    "bMoving", IsoUtils.DistanceManhatten(isoZombie.networkAI.targetX, isoZombie.networkAI.targetY, isoZombie.nx, isoZombie.ny) > 0.5F
                );
            } else {
                if (!isoZombie.networkAI.usePathFind) {
                    pathFindBehavior2.pathToLocationF(isoZombie.networkAI.targetX, isoZombie.networkAI.targetY, (float)isoZombie.networkAI.targetZ);
                    pathFindBehavior2.walkingOnTheSpot.reset(isoZombie.x, isoZombie.y);
                    isoZombie.networkAI.usePathFind = true;
                }

                PathFindBehavior2.BehaviorResult behaviorResult = pathFindBehavior2.update();
                if (behaviorResult == PathFindBehavior2.BehaviorResult.Failed) {
                    isoZombie.setPathFindIndex(-1);
                    return;
                }

                if (behaviorResult == PathFindBehavior2.BehaviorResult.Succeeded) {
                    int int0 = (int)isoZombie.getPathFindBehavior2().getTargetX();
                    int int1 = (int)isoZombie.getPathFindBehavior2().getTargetY();
                    IsoChunk isoChunk = GameServer.bServer
                        ? ServerMap.instance.getChunk(int0 / 10, int1 / 10)
                        : IsoWorld.instance.CurrentCell.getChunkForGridSquare(int0, int1, 0);
                    if (isoChunk == null) {
                        isoZombie.setVariable("bMoving", true);
                        return;
                    }

                    isoZombie.setPath2(null);
                    isoZombie.setVariable("bMoving", true);
                    return;
                }
            }
        } else {
            if (isoZombie.networkAI.usePathFind) {
                pathFindBehavior2.reset();
                isoZombie.setPath2(null);
                isoZombie.networkAI.usePathFind = false;
            }

            pathFindBehavior2.moveToPoint(isoZombie.networkAI.targetX, isoZombie.networkAI.targetY, 1.0F);
            isoZombie.setVariable(
                "bMoving", IsoUtils.DistanceManhatten(isoZombie.networkAI.targetX, isoZombie.networkAI.targetY, isoZombie.nx, isoZombie.ny) > 0.5F
            );
        }

        if (!((IsoZombie)owner).bCrawling) {
            owner.setOnFloor(false);
        }

        boolean _boolean = owner.isCollidedWithVehicle();
        if (isoZombie.target instanceof IsoGameCharacter
            && ((IsoGameCharacter)isoZombie.target).getVehicle() != null
            && ((IsoGameCharacter)isoZombie.target).getVehicle().isCharacterAdjacentTo(owner)) {
            _boolean = false;
        }

        if (owner.isCollidedThisFrame() || _boolean) {
            isoZombie.AllowRepathDelay = 0.0F;
            isoZombie.pathToLocation(owner.getPathTargetX(), owner.getPathTargetY(), owner.getPathTargetZ());
            if (!"true".equals(isoZombie.getVariableString("bPathfind"))) {
                isoZombie.setVariable("bPathfind", true);
                isoZombie.setVariable("bMoving", false);
            }
        }

        HashMap hashMap = owner.getStateMachineParams(this);
        long _long = (Long)hashMap.get(PARAM_TICK_COUNT);
        if (IngameState.instance.numberTicks - _long == 2L) {
            isoZombie.parameterZombieState.setState(ParameterZombieState.State.Idle);
        }
    }

    @Override
    public void exit(IsoGameCharacter owner) {
        owner.setVariable("bMoving", false);
    }
}
